<?php

namespace app\index\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

class User extends Common
{
    /**
     * 用户信息
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        if (Session::has('id') === false) {
            return redirect("/index/user/login");
        }
        $uid = Session::get("id");
        $user = Db::table("shop_user")->where("id",$uid)->find();
        MyViewAssign("user",$user);
        return MyView();
    }

    /**
     * 用户登录
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login()
    {
        if (Request::method() == "POST"){
            $all = Request::param();
            $user = Db::table('shop_user')->where('username',$all['username'])->find();
            if(empty($user)){  //查询不到记录
                echo json_encode(['code'=>1,'msg'=>'用户名不存在']);
                exit;
            }
            if(md5($all['password'])!=$user['password']){  //md5加密比较
                echo json_encode(['code'=>1,'msg'=>'密码错误']);
                exit;
            }
            $cartOld = Session::has("carts") ? Session::get("carts") : [];
            foreach ($cartOld as $goodId => $num){
                $cart = Db::table("shop_cart")->where("goods_id",$goodId)->where("user_id",$user["id"])->find();
                if ($cart){
                    $cart["num"] += $num;
                    Db::table("shop_cart")->where("id",$cart["id"])->update($cart);
                }else{
                    $data["user_id"] = $user["id"];
                    $data["goods_id"] = $goodId;
                    $data["num"] = $num;
                    $data["status"] = 1;
                    $data["add_time"] = $data["up_time"] = time();
                    Db::table("shop_cart")->insert($data);
                }
            }
            Session::set("user",$user);
            Session::set("id",$user['id']);
            Session::set("username",$user['username']);
            echo json_encode(['code'=>0,'msg'=>'登录成功']);
        }else{
            return MyView();
        }
    }

    /**
     * 用户注册
     * @return string|void
     */
    public function register()
    {
        if (Request::method() == "POST"){
            $all = Request::param();
            $all["password"] = md5($all['password']);
            $all['add_time'] = $all['up_time'] = time();
            $insert = Db::table('shop_user')->insert($all);
            if($insert){    //成功
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
                exit;
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'添加失败']);
                exit;
            }
        }
        return MyView();
    }

    /**
     * 用户信息编辑
     * @throws \think\db\exception\DbException
     */
    public function edit()
    {
        if (Request::method() == "POST"){
            $all = Request::param();
            if ($all["nickname"] == ""){
                echo '<script>alert("请填写昵称");window.location.href = "/index/user/index"</script>';
            }
            $data['nickname'] = $all["nickname"];
            $data["gender"] = $all["gender"];
            $data['add_time'] = $data["up_time"] = time();
            $update = Db::table("shop_user")->where("id",$all['id'])->update($data);
            if($update){    //成功
                echo '<script>alert("更新成功");window.location.href = "/index/user/index"</script>';
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'添加失败']);
                exit;
            }
        }
    }

    /**
     * 退出登录
     * @return \think\response\Redirect
     */
    public function loginout()
    {
        Session::delete("user");
        Session::delete("id");
        Session::delete("username");
        cookie(Session::getName(),null);
        Session::clear();
        Session::destroy();
        echo json_encode(['code'=>0,'msg'=>'退出成功']);
    }

}